using System;

namespace NAsserter.Impl
{
    public class BetweenExpression<T> : BaseChainedExpression<T>, IBetweenExpression<T>
    {
        internal BetweenExpression(IArgumentExpressions<T> parent, T value) : base(parent, value) { }

        public virtual IArgumentExpressions<T> And(T to)
        {
            if (AssertThat.IsValid(Parent.Value) && AssertThat.IsValid(Parent.Value) && AssertThat.IsValid(Value) && AssertThat.IsValid(to) && 
                (AssertThat.Compare(Parent.Value, Value) < 0 || AssertThat.Compare(Parent.Value, to)  > 0))
            {
                throw new ArgumentOutOfRangeException(
                    Parent.Name, 
                    string.Format("Expected the argument ({0}) to be within range between {1} and {2}.  Actual value was {3}", Parent.Description, Value, to, Parent.Value));
            }

            return Parent;
        }
    }
}